//
//  DestinationCityViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "DestinationCityViewController.h"
#import "DestinationCityManager.h"
#import "DestinationScenicViewController.h"


@interface DestinationCityViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *cityDataArray;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) DDIndicator *indicator;
@property (nonatomic, strong) UIView *bgView;

@end

@implementation DestinationCityViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"reloadData" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.cityDataArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    DestinationCityManager *manager = [[DestinationCityManager alloc] init];
     [self addSubViews];
    [manager RequestDataByStringReloadWithBlock:^(NSMutableArray *array) {
        self.cityDataArray = array;
        [self StopLoading];
        [self.collectionView reloadData];
        } urlString:self.urlString];
   
    [self addInternetObserver];
}

// 通知网络状态
- (void)addInternetObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDataWithInternet:) name:@"reloadData" object:nil];
}

- (void)reloadDataWithInternet:(NSNotification *)notification
{
     DestinationCityManager *manager = [[DestinationCityManager alloc] init];
    [manager RequestDataByStringReloadWithBlock:^(NSMutableArray *array) {
        self.cityDataArray = array;
        [self StopLoading];
        [self.collectionView reloadData];
        
    } urlString:self.urlString];
}


- (void)addSubViews
{
    [self setNavigationBar];
    [self addCollectionView];
    [self addLoading];
}

- (void)setNavigationBar
{
    self.navigationItem.title = @"目的地列表";
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backNew.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionLeft:)];
    
    self.navigationItem.leftBarButtonItem = leftButton;

}

- (void)actionLeft:(UIBarButtonItem *)leftButton
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)addCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置行间距
    layout.minimumLineSpacing = 5;
    //layout.minimumInteritemSpacing = 10;
    // 设置item的宽高
    layout.itemSize = CGSizeMake((self.view.width - 30) / 2, (self.view.width - 70) / 2);
    // 设置滑动方向 上下滑动
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
   // 设置内边距
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    // 利用layout创建一个视图
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    // 设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    self.collectionView = collectionView;
    // 显示视图
    [self.view addSubview:self.collectionView];
    
    // 注册cell
    [collectionView registerClass:[DestinationCityCell class] forCellWithReuseIdentifier:@"destinationCityCell"];
}

#pragma mark --collectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.cityDataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DestinationCityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"destinationCityCell" forIndexPath:indexPath];
    if (self.cityDataArray.count > 0) {
        DestinationCityModel *model = self.cityDataArray[indexPath.row];
        cell.model = model;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    DestinationCityModel *model = self.cityDataArray[indexPath.row];
    DestinationScenicViewController *dsVC = [[DestinationScenicViewController alloc] init];
    dsVC.Id = model.ID;
    dsVC.tempUrl = model.url;
    dsVC.imageUrl = model.cover;
    [self.navigationController pushViewController:dsVC animated:YES];
    
}

#pragma mark -- 等待动画
- (void)addLoading
{
    // 添加等待动画
    
    self.bgView.hidden = NO;
    
    [self.collectionView addSubview:self.bgView];
    
    self.indicator = [[DDIndicator alloc] initWithFrame:CGRectMake((self.bgView.width - 40) / 2, (self.bgView.height - 40) / 2, 40, 40)];
    [self.bgView addSubview:self.indicator];
    
    [self.indicator startAnimating];
}

- (void)StopLoading
{
    self.bgView.hidden = YES;
    [self.indicator stopAnimating];
}


#pragma mark -- bgView的懒加载
- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake((self.collectionView.width - 100) / 2, (self.collectionView.height - 100) / 2, 100, 100)];
      //  _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
